#!/usr/bin/python

import locale
import os
# import pymysql
from typing import Dict

import config  # noqa: F401
from flask import Flask, Response, send_from_directory
from flask_calendar.actions import (
    delete_task_action,
    do_login_action,
    edit_task_action,
    # hide_repetition_task_instance_action,
    index_action,
    login_action,
    main_calendar_action,
    new_task_action,
    save_task_action,
    update_task_action,
    update_task_day_action,
    search,
)
from flask_calendar.app_utils import task_details_for_markup


def create_app(config_overrides: Dict = None) -> Flask:
    app = Flask(__name__)
    app.config.from_object("config")
    if config_overrides is not None:
        app.config.from_mapping(config_overrides)

    if app.config["LOCALE"] is not None:
        try:
            locale.setlocale(locale.LC_ALL, app.config["LOCALE"])
        except locale.Error as e:
            app.logger.warning("{} ({})".format(str(e), app.config["LOCALE"]))

    # To avoid main_calendar_action below shallowing favicon requests and generating error logs
    @app.route("/favicon.ico")
    def favicon() -> Response:
        return send_from_directory(
            os.path.join(app.root_path, "static"), "favicon.ico", mimetype="image/vnd.microsoft.icon",
        )

    app.add_url_rule("/", "index_action", index_action, methods=["GET"])
    app.add_url_rule("/login", "login_action", login_action, methods=["GET"])
    app.add_url_rule("/do_login", "do_login_action", do_login_action, methods=["POST"])
    app.add_url_rule("/<calendar_id>/", "main_calendar_action", main_calendar_action, methods=["GET"])
    app.add_url_rule(
        "/<calendar_id>/<year>/<month>/new_task", "new_task_action", new_task_action, methods=["GET"],
    )
    app.add_url_rule(
        "/<calendar_id>/<year>/<month>/<day>/<task_id>/", "edit_task_action", edit_task_action, methods=["GET"],
    )
    app.add_url_rule(   # create task
        "/<calendar_id>/<year>/<month>/<day>/task/<task_id>",
        "update_task_action",
        update_task_action,
        methods=["POST"],
    )
    app.add_url_rule(   # create task
        "/<calendar_id>/new_task", "save_task_action", save_task_action, methods=["POST"],
    )
    app.add_url_rule(
        "/<calendar_id>/<year>/<month>/<day>/<task_id>/", "delete_task_action", delete_task_action, methods=["DELETE"],
    )
    app.add_url_rule(
        "/<calendar_id>/<year>/<month>/<day>/<task_id>/",
        "update_task_day_action",
        update_task_day_action,
        methods=["PUT"],
    )
    app.add_url_rule(
        "/<calendar_id>/search/", "search", search, methods=["POST"],
    )
    
    # app.add_url_rule(
    #     "/<calendar_id>/<year>/<month>/<day>/<task_id>/hide/",
    #     "hide_repetition_task_instance_action",
    #     hide_repetition_task_instance_action,
    #     methods=["POST"],
    # )

    app.jinja_env.filters["task_details_for_markup"] = task_details_for_markup

    return app


if __name__ == "__main__":
    # conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', database='calender', charset='utf8')
    # cur = conn.cursor() 
    #  # 此处不需要调用，调用也没啥用，只需要在一个文件中专门写数据库调用的函数，在全局定义这部分，在外面调用操作数据库的函数即可
    app = create_app()
    app.run(debug=app.config["DEBUG"], host=app.config["HOST_IP"])
